﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Map {

	#region properties

	protected MapGrid[,] m_grids;

	protected int m_mapRow, m_mapColumn;
	public int MapRow{get{ return m_mapRow;}}
	public int MapColumn {get {return m_mapColumn;}}

	public static Vector3 MapLeftDownPos = Vector3.zero;		//pos of map[0,0]


	//进入地图时,角色在地图上哪个格子(row, column)
	protected IntVector2 m_startIndex;
	public IntVector2 StartIndex
	{
		get{return m_startIndex;}
		set{
			if(value.x >=0 && value.y >=0)
			{
				m_startIndex = value;

			}
		}
	}

	#endregion

	public Map(int row, int column)
	{
		if(row > 0 && column > 0)
		{
			m_mapRow = row;
			m_mapColumn = column;
			m_grids = new MapGrid[m_mapRow, m_mapColumn];
		}
		StartIndex = IntVector2.zero;
	}

	/// <summary>
	/// Gets the grid by index.
	/// </summary>
	/// <returns>The map grid.</returns>
	/// <param name="row">Row.</param>
	/// <param name="column">Column.</param>
	public MapGrid GetMapGrid(int row, int column)
	{
		if(row >= 0 && row < m_mapRow &&
			column >= 0 && column < m_mapColumn)
		{
			return m_grids[row, column];
		}
		return null;
	}

	public void SetMapGrid(MapGrid grid, int row, int column)
	{
		if(row >= 0 && row < m_mapRow &&
			column >= 0 && column < m_mapColumn)
		{
			m_grids[row, column] = grid;
		}
	}

	/// <summary>
	/// Gets the map grid by pos.
	/// </summary>
	/// <returns>The map grid.</returns>
	/// <param name="pos">Position.</param>
	public MapGrid GetMapGrid(Vector3 pos)
	{
		int row = (int)( (pos.y-MapLeftDownPos.y)/MapGrid.GridHeight );
		int column = (int)( (pos.x-MapLeftDownPos.x)/MapGrid.GridWidth );

		return GetMapGrid(row, column);
	}
}
